﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtorsPortal.Data_Access_Layer;
using System.Data;

namespace RealtorsPortal.AdminCP
{
    public partial class PaymentHistory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LoadAndFillData();
            }
        }

        private void LoadAndFillData()
        {
            DataAccessHelper dah = new DataAccessHelper();
            string dateFrom = txtFrom.Text;
            string dateTo = txtTo.Text;
            bool isExpired = chkIsExpired.Checked;
            string cmd ="";
            if (dateFrom != "")
            {
                if (dateTo == "")
                {
                    dateTo = DateTime.Now.ToShortDateString();
                }
                if (!isExpired)
                {
                    cmd = string.Format(@"exec sp_searchPaymentHistory '{0}', '{1}'", dateFrom, dateTo);
                }
                else
                {
                    cmd = string.Format(@"exec SP_SearchPaymentHistoryWithExpired '{0}', '{1}'", dateFrom, dateTo);
                }
            }
            else
            {
                if (!isExpired)
                {
                    cmd = @"select distinct s.SubId, s.SubDate, u.Username,p.PackageName, p.Price, s.IsPaid
                           from Subscription as s, Packages as p, Users as u, Advertisements as a
                           where s.PackageId = p.PackageId and s.UserId = u.UserId and a.PackageId = p.PackageId";
                }
                else
                {
                    cmd += " and a.ExpDate < GETDATE()";
                }
            }
            DataTable dt = dah.GetDataTable(cmd);
            grvPaymentHistory.DataSource = dt;
            grvPaymentHistory.DataBind();

        }

        protected void grvPaymentHistory_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvPaymentHistory.PageIndex = e.NewPageIndex;
            this.LoadAndFillData();
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            grvPaymentHistory.PageSize = int.Parse(ddlPageSize.Text);
            this.LoadAndFillData();
        }

        protected void grvPaymentHistory_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = grvPaymentHistory.DataSource as DataTable;

            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);

                grvPaymentHistory.DataSource = dataView;
                grvPaymentHistory.DataBind();
            }
        }

        private string ConvertSortDirection(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }

            return newSortDirection;
        }

        protected void lbtnSearch_Click(object sender, EventArgs e)
        {

            this.LoadAndFillData();
        }

        protected void grvPaymentHistory_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName != String.Empty)
            {
                int index = Convert.ToInt32(e.CommandArgument);

                switch (e.CommandName.ToString())
                {
                    case "Browse":
                        ViewDetailPayment(index);
                        break;
                }
            }
        }

        private void ViewDetailPayment(int index)
        {
            int subId = int.Parse(grvPaymentHistory.DataKeys[index].Value.ToString());
            DataAccessHelper dah = new DataAccessHelper();
            string cmd = @"select a.AdvId
                        from Subscription as s, Packages as p, Users as u, Advertisements as a
                        where s.PackageId = p.PackageId and s.UserId = u.UserId and p.PackageId = a.PackageId
                        and SubId = " + subId;
            string advId = dah.GetValue(cmd).ToString();
            Response.Redirect("../Detail.aspx?AdvId=" + advId);

        }
    }
}